﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity.Car_Rental;
using Entity.CommonEnums;
namespace Data.Car_Rental
{
    public class BlockRepository : Car_RentalRepositoryBase, IBlockRepository, Data.IBackgroundRepository<Block>
    {

        #region IRepository<Block> 成员

        public void Update(Block entity)
        {
            base.UpdateEntity(entity);
        }

        public void Update(IList<Block> list)
        {
            base.UpdateEntity(list.Cast<Entity.EntityBase>().ToList());
        }

        public void Insert(Block entity)
        {
            base.InsertEntity(entity);
        }

        public void Insert(IList<Block> list)
        {
            base.InsertEntity(list.Cast<Entity.EntityBase>().ToList());
        }

        public Block InsertGetIDENTITY(Block entity)
        {
            throw new NotImplementedException();
        }

        public void Delete(Block entity)
        {
            base.DeleteEntity(entity);
        }

        public void Delete(IList<Block> list)
        {
            base.DeleteEntity(list.Cast<Entity.EntityBase>().ToList());
        }

        public IQueryable<Block> GetModel()
        {
            return base.GetEntities<Block>().Select(i => new Block_Ext
            {
                BlockID = i.BlockID,
                BlockName = i.BlockName,
                BlockURL = i.BlockURL,
                SortNumber = i.SortNumber,
                Status = i.Status,
                Type = i.Type,
            });
        }

        public Block Find(params object[] keyValues)
        {
            return base.Find<Block>(keyValues);
        }

        #endregion

        #region IBlockRepository 成员

        public IQueryable<Block> GetModelForSite()
        {
            return this.GetModel().Where(i => i.Status == (int)Status.Normal);
        }

        public IQueryable<Block> GetModelForBackground()
        {
            return this.GetModel();
        }

        #endregion

        #region Static List
        /// <summary>
        /// 实体结构集
        /// </summary>
        public static List<Block> blockList;
        static System.Timers.Timer sysTimer;
        static BlockRepository()
        {
            sysTimer = new System.Timers.Timer();
            sysTimer.Enabled = true;
            sysTimer.Interval = 60000;
            sysTimer.AutoReset = true;
            sysTimer.Elapsed += new System.Timers.ElapsedEventHandler(sysTimer_Elapsed);
            sysTimer.Start();
            ReLoad();
        }

        static void sysTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            ReLoad();
        }
        /// <summary>
        /// 加载新的结果集
        /// </summary>
        public static void ReLoad()
        {
            blockList = new BlockRepository().GetModelForSite().ToList();
        }
        #endregion

    }
}
